package com.bjfu.gmms.servlet;


import com.bjfu.gmms.model.StudentExamView;
import com.bjfu.gmms.service.StudentExamViewService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentInfo")
public class StudentExamViewServlet extends HttpServlet {
    private StudentExamViewService studentService;

    @Override
    public void init() throws ServletException {
        studentService = new StudentExamViewService();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String studentIdStr = request.getParameter("studentId");

        try {
            int studentId = Integer.parseInt(studentIdStr);  // 将输入的 ID 转换为整数
            List<StudentExamView> studentExamInfo = studentService.getStudentExamInfo(studentId);
            System.out.println(studentExamInfo.size());
            System.out.println(studentExamInfo);
            request.setAttribute("studentExamInfo", studentExamInfo);
            System.out.println("传递给 JSP 的数据：" + studentExamInfo);
            request.getRequestDispatcher("examresult.jsp").forward(request, response);
        } catch (NumberFormatException e) {
            request.setAttribute("error", "Invalid student ID format.");
            request.getRequestDispatcher("exam.jsp").forward(request, response);
        }
    }
}
